package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.flag.ResourcesPath;
	import engine.logic.SceneTransfer;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.util.FilterUtil;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.config.TextIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;

	public class AdventureTermLogic
	{
		public static var _self:AdventureTermLogic ; 
		public static function getInstance():AdventureTermLogic
		{
			if(_self==null)
			{
				_self = new AdventureTermLogic () ; 
			}
			return _self ; 
		}
		public 	var _ui:AdventureTermUI ; 
		private  var _configID:int  ; 
		private var _model:ModelLocator ; 
		private var _itemGroupCount:int ; 
		/***当前任务的配置**/
		private var _obj:Object ; 
		/****/
		private var nameMC:MovieClip;  
		private var textIni:TextIni =new TextIni(); 
		public function AdventureTermLogic()
		{
			
		}
		public function Init():void
		{
			_ui=AdventureTermUI.getInstance() ; 
			_model = ModelLocator.getInstance() ; 
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ADVENTURE_DETAIL_INFO),getInfo);
		}
		public function beforeShow():void
		{
			_obj =StaticIni.getIniObj(_configID.toString()); 
			_itemGroupCount = _ui.itemGroup.col  * _ui.itemGroup.row ; 
			
			addListener(); 
			itemGroup(); 
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_ADVENTURE_DETAIL_INFO,_configID);
			showTastNpc();
			showAward();
			initScene();
		}
		public function AfterHide():void
		{
			_obj = null ; 
			removeListener();
			_ui.firstly.htmlText = "";  
			
		}
		private function addListener():void
		{
			_ui.itemGroup.addItemsEvent(MouseEvent.MOUSE_OVER , overHandler);  
			_ui.itemGroup .addItemsEvent(MouseEvent.MOUSE_OUT,  outHandler );
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE, colseHandler);
			/**奖品Tips**/
			_ui.ima.addEventListener(MouseEvent.MOUSE_OVER, overImaHandler); 
			_ui.ima.addEventListener(MouseEvent.MOUSE_OUT, outImaHandler); 
			//连接接口
//			_ui.label_2.addLinkClickHandel(onTxtPramLinkClick);
			_ui.text_2.addLinkClickHandel(onTxtPramLinkClick) ; 
			_ui.text_2.setFilter();
		}
		private function colseHandler(e:NxWindowEvent):void
		{
			_ui.showOrHide(); 
		}
		private function removeListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE, colseHandler); 
			_ui.text_2.removeLinkClickHandel();
		}
		/**
		 *初始化进度 
		 */		
		private function itemGroup():void
		{
			if(_configID == 0)return ; 
			
			
			var i :uint =0 ; 
			for( i ; i < _itemGroupCount ;  i ++)
			{
				var item:MovieClip = _ui.itemGroup.getItemByIndex(i);
				if(i<int(_obj["Count"]))
				{
					item.visible =  true; 
					item["pic"].gotoAndStop(13) ;
					item["string"] = "AdventureTips_"+_configID.toString() +"_"+(i+1).toString(); 
					item["AdBeforeTips"]="AdBeforeTips_"+_configID.toString() +"_"+(i+1).toString(); 
					item["data"] = 0 ; 
					item.filters  = [FilterUtil.GrayColorFilter] ; 
					
				}
				else
				{
					item.visible =  false;  
					item.gotoAndStop(1);
				}
				
			}
		}
		//你所点击奇遇的configID 
		public  function set configID(value:int):void
		{
			_configID = value  ; 
		}
		/****/
		public function set nameMc(mc:MovieClip):void
		{
			nameMC = mc  ;
		}
		//获取奇遇完成进度
		private function getInfo(e:CustomMsgEvent):void
		{
			var str:String = e.pList.shift() ; 
			if(str != "")
			{
				_ui.firstly.htmlText = StaticIni.getSwfLangStrVar("UIString_1000681",[str]);
			}else
			{
				_ui.firstly.htmlText = "";
			}
			var arr:Array = soreOn(e.pList) ; 
			var i:int = 0 ; 
			if(_obj==null)
			{
				_obj =StaticIni.getIniObj(_configID.toString()); 
			}
			for( i ; i < _obj["Count"] ;i++)
			{
				var item:MovieClip = _ui.itemGroup.getItemByIndex(i);
				if(i<arr.length)
				{
//					item.visible =  true; 
					if((arr[i] as Object)["type"] != item["data"])
					{
						item.filters  = [] ; 
						item["pic"].gotoAndStop(int(i+1)) ;
						item["data"] = (arr[i] as Object)["type"] ; 
					}
				}
//				else
//				{
//					item.visible =  false; 
//				}
				
			}
		}
		/**
		 * 
		 */
		private function soreOn(arr:Array):Array
		{
			if(arr.length==0||arr[0]=="")return null; 
			var i:uint  = 0 ; 
			var arrList:Array  = [] ;
			for( i ; i <(arr.length-1)/2 ; i++)
			{
				var obj:Object = {} ; 
				obj["id"] = arr[1+(i*2)];
				obj["type"] =  arr[2+(i*2)];
				arrList.push(obj)
			}
			arrList.sortOn("id"); 
			return arrList; 
			
		}
		/**
		 * 鼠标移上去 
		 */		
		private function overHandler(e:MouseEvent):void
		{
			var item:MovieClip = e.currentTarget as MovieClip ; 
			if(item["data"]!=0)
			{
//				TipsMainLogic.instance.showString(StaticIni.getSwfLangStr(item["string"])); 
				TipsMainLogic.instance.showString(getStr(item["string"]));
			}else
			{
				TipsMainLogic.instance.showString(StaticIni.getSwfLangStr(item["AdBeforeTips"])); 
				TipsMainLogic.instance.showString(getStr(item["AdBeforeTips"]));
					
			}
			
		}
		/**
		 * 鼠标移开 
		 */		
		private function outHandler(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 * 奖品
		 */		
		private function showAward():void
		{
			//GetItem 奖品属性
			var obj:Object = StaticIni.getIniObj(_obj["GetItem"]) ;
			if(obj==null)return ; 
			(_ui.ima as NxClip).source =ResourcesPath.getPropsURL(int(obj["Photo"]));
			
		}
		/**
		 * 显示任务接取和完成的NPC 的名字
		 */		
		private function showTastNpc():void
		{
			//触发任务
			_ui.text_1.htmlText  =StaticIni.getSwfLangStr("Task_Caption"+_obj["Task"]); 
			//接取任务NPC
			_ui.text_2.htmlText  ="<font color='#1eff00'>"+"<u>"+StaticIni.getSwfLangStr("Task_AcceptNpc"+_obj["Task"])+"</u>"+"</font>"; 
			
		}
		/**
		 *点击超链接 
		 */
		private function onTxtPramLinkClick(val:String):void
		{
			// 连接接口
			SceneTransfer.getInstance().tranferTo(val);
		}
		/**
		 * 鼠标移到 奖品上去
		 */		
		private function overImaHandler(e:MouseEvent):void
		{
			TipsMainLogic.instance.showMain(int(_obj["GetItem"]))
		}
		/**
		 * 鼠标移出 奖品上去
		 */		
		private function outImaHandler(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**背景图和奇遇名字**/
		private function initScene():void
		{
			_ui.imaback.source  = ResPath.JPG+"AdventureIma.jpg" ; 
			_ui.titleIma.source  =nameMC ; 
			
//			MultLoader.getInstance().load([StageInfo.swfResPath + "IntensifyBG.swf"],true,createScene);
		}
//		private function createScene():void
//		{
//			var mc :MovieClip =  MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath + "IntensifyBG.swf") ; 
//			if(mc)
//			{
//				_ui.windowBG.addChildAt(mc , 1);
//				
//			}else
//			{
//				
//			}
//		}
		public function getStr(value:String):String
		{
			return textIni.getStrContent(AdventureLogic.getInstance().strArr , value ,"name");
		}
	
	}
}